import datacti
import numpy as np
from numpy import linalg as LA

def stot(S):
    S_11 = np.array(S[0,0,:])
    S_12 = np.array(S[0,1,:])
    S_21 = np.array(S[1,0,:])
    S_22 = np.array(S[1,1,:])

    T_11 = -(S_11*S_22-S_12*S_21)/S_21
    T_12 = S_11/S_21
    T_21 = -S_22/S_21
    T_22 = 1./S_21

    return np.array([[T_11,T_12],[T_21,T_22]])

def ttos(T):
    T_11 = np.array(T[0,0,:])
    T_12 = np.array(T[0,1,:])
    T_21 = np.array(T[1,0,:])
    T_22 = np.array(T[1,1,:])

    S_11 = T_12/T_22
    S_12 = (T_11*T_22-T_12*T_21)/T_22
    S_21 = 1./T_22
    S_22 = -T_21/T_22

    return np.array([[S_11,S_12],[S_21,S_22]])
    


da = datacti.DataCTI()
NoP = 1601# Number of points

# Load 2-port measurement of the device
S_11 = np.array(da.load('data/TL-2p.cti',NoP)['BEGIN0'])
S_21 = np.array(da.load('data/TL-2p.cti',NoP)['BEGIN1'])
S_12 = np.array(da.load('data/TL-2p.cti',NoP)['BEGIN2'])
S_22 = np.array(da.load('data/TL-2p.cti',NoP)['BEGIN3'])

S_device = np.array([[S_11,S_12],[S_21,S_22]])

T_device = stot(S_device)

# Model Probe

S_load = np.array(da.load('data/Pr-load.cti',NoP)['BEGIN0'])
S_short = np.array(da.load('data/Pr-short.cti',NoP)['BEGIN0'])
S_open = np.array(da.load('data/Pr-open.cti',NoP)['BEGIN0'])

S_p22 = (S_open - 2*S_load + S_short)/(S_open - S_short)
S_p12 = -2**(1./2)*(-((S_load - S_open)*(S_load - S_short))/(S_open - S_short))**(1./2)

S_probe = np.array([[S_load,S_p12],[S_p12,S_p22]])


"""
S_p11 = np.array(da.load('TL_SMA_END_probe_deim.cti',NoP)['BEGIN0'])
S_p21 = np.array(da.load('TL_SMA_END_probe_deim.cti',NoP)['BEGIN1'])
S_p12 = np.array(da.load('TL_SMA_END_probe_deim.cti',NoP)['BEGIN2'])
S_p22 = np.array(da.load('TL_SMA_END_probe_deim.cti',NoP)['BEGIN3'])

S_probe = np.array([[S_p11,S_p12],[S_p21,S_p22]])
"""

T_probe = stot(S_probe)

T_deem = np.empty([2,2,NoP],dtype=complex)

for i in range(0,T_probe.shape[2]):
    T_deem[:,:,i] = np.dot(T_device[:,:,i],\
                           LA.inv(T_probe[:,:,i]))

S_deem = ttos(T_deem)

stop = 1e9
start = 0.5e9
steps = NoP-1
step = (stop-start)/steps
start2 = start

lw = []

lw += ['CITIFILE A.01.01\r\n']
lw += ['!\r\n']
lw += ['NAME CH1_DATA\r\n']
lw += ['VAR Freq MAG '+`NoP`+'\r\n']
lw += ['DATA S[1,1] RI\r\n']
lw += ['DATA S[2,1] RI\r\n']
lw += ['DATA S[1,2] RI\r\n']
lw += ['DATA S[2,2] RI\r\n']
lw += ['VAR_LIST_BEGIN\r\n']
lw += [`int(start)`+'\r\n']
for i in range(0,NoP-1):
    lw += [`int(start2+step)`+'\r\n']
    start2 = start2+step
lw += ['VAR_LIST_END\r\n']
lw += ['BEGIN\r\n']
for i in range(0,NoP):
    lw += [`S_deem[0,0,i].real`+','+`S_deem[0,0,i].imag`+'\r\n']
lw += ['END\r\n']
lw += ['BEGIN\r\n']
for i in range(0,NoP):
    lw += [`S_deem[1,0,i].real`+','+`S_deem[1,0,i].imag`+'\r\n']
lw += ['END\r\n']
lw += ['BEGIN\r\n']
for i in range(0,NoP):
    lw += [`S_deem[0,1,i].real`+','+`S_deem[0,1,i].imag`+'\r\n']
lw += ['END\r\n']
lw += ['BEGIN\r\n']
for i in range(0,NoP):
    lw += [`S_deem[1,1,i].real`+','+`S_deem[1,1,i].imag`+'\r\n']
lw += ['END\r\n']
open('TL-probe-m.cti','wb').writelines(lw)

